import java.util.Arrays;

import Basics.Atom;
import Basics.Program;
import Basics.Rule;
import Basics.Var;
import Pattern.PatternNode;



public class ParseProgram 
{
	public static void main (String [] args)
	{
		String str = "a(1,2) :- b(3,4) , c(5,6) & 0.45 \n k(1,2) :- g(3,4) , h(5,6) & 0.6";
		System.out.println(BuildProgram(str));	
	}


	/*************************************************************************************************************/
	/** Title: BuildProgram																				
	/** Description: Checks that the user input is of legal form 			
	/*************************************************************************************************************/
	
	private static Program BuildProgram (String str)
	{
		String [] strRules = str.split("\n");
		Rule [] rules = new Rule [strRules.length];
		for (int i = 0; i < rules.length; i++) 
		{
			rules[i] = BuildRule(strRules[i]);
		}
		
		return new Program(rules);
	}
	
	
	
	
	/*************************************************************************************************************/
	/** Title: BuildRule																				
	/** Description: Takes a string and turns it into a Rule 			
	/*************************************************************************************************************/
	
	private static Rule BuildRule (String str)
	{
		String [] sepWeight = str.split("&");
		String [] strAtoms = sepWeight[0].split("\\s(?=\\()|(?<=\\))\\s");
		Atom [] atoms = new Atom [strAtoms.length];
		double weight = Double.parseDouble(sepWeight[1]);
		
		//make atoms
		for (int i = 0; i < strAtoms.length; i++)
		{
			String[] nameParams = strAtoms[i].split("\\(");
			String name = nameParams[0].replaceAll("[^A-Za-z]+", "");
			nameParams[1] = nameParams[1].substring(0, nameParams[1].length()-1);
			String[] params = nameParams[1].split(",");
			//atoms[i] = BuildAtom (name, params);
		}
		
		return new Rule (atoms[0], weight, Arrays.copyOfRange(atoms, 1, atoms.length));
	}
	
	
	
	
	/*************************************************************************************************************/
	/** Title: BuildAtom																				
	/** Description: Takes a name string and an array of parameters and turns it into an Atom 			
	/*************************************************************************************************************/
	/*
	private static Atom BuildAtom (String name, String [] params)
	{
		Var [] vars = new Var [params.length];
		for (int i = 0; i < params.length; i++) 
		{
			String cat = (0 == i) ? "Country" : "Product";
			vars[i] = new Var(params[i], cat); 
		}
		
		return new Atom (name, vars);
	}
	*/
	
	
	/*************************************************************************************************************/
	/** Title: BuildPatternNode																				
	/** Description: Takes a name string and an array of parameters and turns it into an PatternNode 			
	/*************************************************************************************************************/
	
	public static PatternNode BuildPatternNode (String text)
	{
		String[] nameParams = text.split("\\(");
		String name = nameParams[0].replaceAll("[^A-Za-z]+", "");
		nameParams[1] = nameParams[1].substring(0, nameParams[1].length()-1);
		String[] params = nameParams[1].split(",");
		
		Var [] vars = new Var [params.length];
		for (int i = 0; i < params.length; i++) 
		{
			String cat = (0 == i) ? "Country" : "Product";
			vars[i] = new Var(params[i], cat); 
		}
		
		return new PatternNode (name, false, vars);
	}
}
